package com.roadjava.demo08.util;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.executable.ExecutableValidator;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class ValidationUtil8 {

    private static ExecutableValidator executableValidator;

    static {
        // 校验入参或返回值的
        executableValidator = Validation.buildDefaultValidatorFactory()
                .getValidator()
                .forExecutables();
    }

    public static <T> List<String> validNotBean(T object,
                                                Method method,
                                                Object[] parameterValues,
                                                Class<?>... groups) {

        Set<ConstraintViolation<T>> set = executableValidator.validateParameters(object, method, parameterValues, groups);

        List<String> list = set.stream().map(v ->
                        "属性：" + v.getPropertyPath()
                                + "，属性的值：" + v.getInvalidValue()
                                + "，校验不通过的提示信息：" + v.getMessage())
                .collect(Collectors.toList());
        return list;
    }
}
